/**
 * 工具
 * @type {{}}
 */
function HdUtil() {

};
//常量
HdUtil.constant = {
    INFO: "提示",
    ERROR: "错误",
    WARINING: "警告",
    CONFIRM: "确认",
    SAVE_SUCCESS: "保存成功",
    DELETE_CONFIRM: "确认删除吗",
    DELETE_SUCCESS: "删除成功",
    SELECT_EDIT_ROW: "请选择要编辑的行",
    SELECT_CARGOINOUT_ROW: "选中记录需为进库且包含一程船信息！"

};
/**
 * easyui常用弹框
 * @type {{}}
 */
HdUtil.messager = {
    info: function (msg) {
        $.messager.alert(HdUtil.constant.INFO, msg, "info");
    },
    error: function (msg) {
        $.messager.alert(HdUtil.constant.ERROR, msg, "error");
    },
    warning: function (msg) {
        $.messager.alert(HdUtil.constant.WARINING, msg, "warning");
    },
    slide: function (msg) {
        $.messager.show({
            title: HdUtil.constant.INFO,
            msg: msg,
            timeout: 2000,
            showType: 'slide'
        });
    }
};
HdUtil.formatter = {
    /**
     * 数字格式化
     * @param number 要格式化的数字
     * @param digit 保留几位小数
     * @returns {string}
     */
    numberFormat: function format(number, digit) {
        var n = parseFloat(number).toFixed(digit);
        var re = /(\d{1,3})(?=(\d{3})+(?:\.))/g;
        return n.replace(re, "$1,");
    }
};
HdUtil.date = {
    getCurrentDate: function () {
        return new Date().format("yyyy-MM-dd");
    },
    getCurrentDateTime: function () {
        return new Date().format("yyyy-MM-dd hh:mm:ss");
    },
    /**
     * 获取本周第一天日期
     */
    getCurrentWeekBeginDate: function () {
        var now = new Date();
        return new Date(now - (now.getDay() - 1) * 86400000).format("yyyy-MM-dd");
    },
    /**
     * 获取本月第一天日期
     */
    getCurrentMonthBeginDate: function () {
        var now = new Date();
        now.setDate(1);
        return now.format("yyyy-MM-dd");
    },
    /**
     * 获取上月第一天日期
     */
    getLastMonthBeginDate: function () {
        var now = new Date();
        now.setDate(1);
        return new Date(now.setMonth(now.getMonth() - 1)).format("yyyy-MM-dd");
    },
    /**
     * 获取上月最后一天日期
     */
    getLastMonthEndDate: function () {
        var now = new Date();
        now.setDate(1);
        return new Date(now.setDate(now.getDate() - 1)).format("yyyy-MM-dd");
    }
};
HdUtil.combobox = {
    /**
     * 用户可查询的码头列表
     * @param params
     */
    userCompany: function (params) {
        var ret = {
            url: "../../SystemDeptCompany/getCompanyListByLoginUser",
            limitToList:true,
            editable:false,
            valueField: "code",
            textField: "name",
            onLoadSuccess:function (data) {
                if(data&&data.length>0){
                    $(this).combobox('select',data[0].code);
                }
            }
        }
        return $.extend(ret, params);
    }
};
HdUtil.combogrid = {
    zwcm: function (params) {
        var ret = {
            url: "../../BaYscb/getAllYscb",
            idField: 'ccdjh',
            textField: 'zwcm',
            pagination: true,
            sortName: "ccdjh",
            panelWidth: 470,
            panelHeight: 320,
            sortOrder: "asc",
            mode: "remote",
            columns: [[
                {
                    field: 'ccdjh',
                    title: '初次登记号',
                    halign: "center",
                    width: 130
                },
                {
                    field: 'zwcm',
                    title: '中文船名',
                    halign: "center",
                    width: 100
                },
                {
                    field: 'cjgmc',
                    title: '船籍港',
                    halign: "center",
                    width: 100
                },
                {
                    field: 'cblxmc',
                    title: '船舶类型',
                    halign: "center",
                    width: 100
                }
            ]],
            onHidePanel: function () {
                var val = $(this).combogrid("getValue");
                var valueField = $(this).combogrid("options").idField;
                var grid = $(this).combogrid('grid');
                var allData = grid.datagrid("getData").rows;
                var result = true;
                for (var i = 0; i < allData.length; i++) {
                    if (val == allData[i][valueField]) {
                        result = false;
                    }
                }
                if (result) {
                    $(this).combogrid("setValue", "");
                    grid.datagrid({
                        queryParams: {}
                    });
                    grid.datagrid("reload");
                }
            }
        };
        return $.extend(ret, params);
    },
    dwmc: function (params) {
        var ret = {
            url: "../../BaJsdw/getJsdwByDwlx",
            idField: 'dwdm',
            textField: 'dwmc',
            pagination: true,
            sortName: "dwmc",
            panelWidth: 570,
            panelHeight: 320,
            sortOrder: "asc",
            mode: "remote",
            columns: [[
                {
                    field: 'dwdm',
                    title: '单位代码',
                    halign: "center",
                    width: 200
                },
                {
                    field: 'dwmc',
                    title: '单位名称',
                    halign: "center",
                    width: 200
                },
                {
                    field: 'bsr',
                    title: '办税人',
                    halign: "center",
                    width: 80
                },
                {
                    field: 'dwlxmc',
                    title: '单位类型',
                    halign: "center",
                    width: 80
                }
            ]],
            onHidePanel: function () {
                var val = $(this).combogrid("getValue");
                var valueField = $(this).combogrid("options").idField;
                var grid = $(this).combogrid('grid');
                var allData = grid.datagrid("getData").rows;
                var result = true;
                for (var i = 0; i < allData.length; i++) {
                    if (val == allData[i][valueField]) {
                        result = false;
                    }
                }
                if (result) {
                    $(this).combogrid("setValue", "");
                    grid.datagrid({
                        queryParams: {}
                    });
                    grid.datagrid("reload");
                }
            }
        };
        return $.extend(ret, params);
    }
};
Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(), //day
        "h+": this.getHours(), //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    return format;
};
/**
 *
 * @requires jQuery
 *
 * 将form表单元素的值序列化成对象
 *
 * @returns object
 */
$.serializeObject = function (form) {
    var o = {};
    $.each(form.serializeArray(), function (index) {
        if (o[this['name']]) {
            o[this['name']] = o[this['name']] + "," + this['value'];
        } else {
            o[this['name']] = this['value'];
        }
    });
    return o;
};
$.ajaxSetup({
    cache: false,
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        switch (XMLHttpRequest.status) {
            case(401):
                alert("没有权限！");
                break;
        }
    }
});